package api;

import utils.CommUtils;
import utils.HttpUtils;

import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

/**
 * 人脸比对，人脸视频检活，人脸核身等API接口
 */
public class LiveFaceApi extends AccessToken {
    private final static String faceImageURI = "/aip/face/v1/compare/face-image";
    private final static String faceVideoLiveURI = "/aip/face/v1/compare/face-video-liveness";
    private final static String faceVideoLiveStrictlyURI = "/aip/face/v1/compare/face-video-liveness-strictly";
    private final static String faceVideoLiveNumberURI = "/aip/face/v1/compare/face-video-liveness-number";
    private final static String faceVideoLiveCheckIdURI = "/aip/face/v1/compare/face-video-liveness-checkid";


    /**
     * 1.照片比对
     */
    public static void imageCompare(String _BizNo, String imageA, String imageB) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("imageA", imageA);
        paramsMap.put("imageB", imageB);
        paramsMap.put("_BizNo", _BizNo);
        paramsMap.put("sign", CommUtils.sign(paramsMap, secretKey));
        String res = HttpUtils.doPostJSON(host, faceImageURI, getCachedAccessToken(), paramsMap);
        System.out.println("照片比对-- >> imageCompare:" + res);
    }

    /**
     * 2.人脸视频检活及对比-普通模式
     */
    public static void faceVideoLiveness(String _BizNo, String image, String video) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("image", image);
        paramsMap.put("video", video);
        paramsMap.put("_BizNo", _BizNo);
        paramsMap.put("sign", CommUtils.sign(paramsMap, secretKey));
        String res = HttpUtils.doPostJSON(host, faceVideoLiveURI, getCachedAccessToken(), paramsMap);
        System.out.println("人脸视频检活及对比-普通模式-- >> faceVideoLiveness:" + res);

    }


    /**
     * 3.人脸视频检活及对比-严格模式,可返回最佳视频帧
     */
    public static void faceVideoLiveStrictly(String _BizNo, String image, String video) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("image", image);
        paramsMap.put("video", video);
        paramsMap.put("isReturnBestFace", 1);
        paramsMap.put("_BizNo", _BizNo);
        paramsMap.put("sign", CommUtils.sign(paramsMap, secretKey));
        String res = HttpUtils.doPostJSON(host, faceVideoLiveStrictlyURI, getCachedAccessToken(), paramsMap);
        System.out.println("人脸视频检活及对比-严格模式-- >> faceVideoLiveStrictly:" + res);
    }


    /**
     * 4.人脸视频检活及对比-读数字模式
     */
    public static void faceVideoLiveNumber(String _BizNo, String image, String video, String number) {
        Map<String, Object> paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("image", image);
        paramsMap.put("video", video);
        paramsMap.put("number", number);
        paramsMap.put("isReturnBestFace", 1);
        paramsMap.put("_BizNo", _BizNo);
        paramsMap.put("sign", CommUtils.sign(paramsMap, secretKey));
        String res = HttpUtils.doPostJSON(host, faceVideoLiveNumberURI, getCachedAccessToken(), paramsMap);
        System.out.println("人脸视频检活及对比-读数字模式-- >> faceVideoLiveNumber:" + res);
    }

    /**
     * 5.人脸视频检活加身份三要素核验
     */
    public static void faceVideoLiveCheckId(String _BizNo, String cardName, String cardCode, String video) {
        Map<String, Object> paramsMap;
        paramsMap = new TreeMap<>();
        paramsMap.put("appId", appId);
        paramsMap.put("video", video);
        paramsMap.put("cardName", cardName);
        paramsMap.put("cardCode", cardCode);
        paramsMap.put("isReturnBestFace", 1);
        paramsMap.put("_BizNo", _BizNo);
        paramsMap.put("sign", CommUtils.sign(paramsMap, secretKey));
        String res = HttpUtils.doPostJSON(host, faceVideoLiveCheckIdURI, getCachedAccessToken(), paramsMap);
        System.out.println("人脸视频检活加身份三要素核验-- >> faceVideoLiveCheckId:" + res);
    }

    public static void main(String[] args) {
        String imageA = CommUtils.fileToBase64Str("H:\\Download\\8db04e8c49682a4ca87d51c24d296972_351102010067517441.jpg");
        String imageB = CommUtils.fileToBase64Str("H:\\Download\\8db04e8c49682a4ca87d51c24d296972_351102010067517441.jpg");
        //1.照片比对
        imageCompare(UUID.randomUUID().toString(), imageA, imageB);

        String image = CommUtils.fileToBase64Str("H:\\Download\\8db04e8c49682a4ca87d51c24d296972_351102010067517441.jpg");
        String video = CommUtils.fileToBase64Str("H:\\Download\\mp4test\\d2313ffd6e4cf80589ee6a85e445fa2c.mp4");
//        String video = CommUtils.fileToBase64Str("H:\\Download\\mp4test\\fake1\\371875210640490497_db1d175eec9e0b979b0c8a054bd4165c.mp4");
        //2.人脸视频检活及对比-普通模式
        faceVideoLiveness(UUID.randomUUID().toString(), image, video);

        //3.人脸视频检活及对比-严格模式,可返回最佳视频帧
        faceVideoLiveStrictly(UUID.randomUUID().toString(), image, video);

        //4.人脸视频检活及对比-读数字模式
        faceVideoLiveNumber(UUID.randomUUID().toString(), image, video, "1234");

        // 5.人脸视频检活+身份三要素核验
        faceVideoLiveCheckId(UUID.randomUUID().toString(), "庞小", "44080419920608021X", video);
    }
}
